let app = getApp()
const util = require('../../utils/util.js')
const { getStuQuestionnaire, getStuTeacher} = require('../../api/geData.js')

Page({
  data:{
    student: app.globalData.userInfo,
    defaultSrc: '/images/mine/mine@2x.png',
    hasQuest: false,
    hasTeachQuest:false,
    userInfo:null
  },
  onLoad(){
    let that = this
    if (Object.keys(app.globalData.userInfo).length == 0){
      app.getUserInfo()
      this.setData({
        student: app.globalData.userInfo
      })
    }
    
    if (app.globalData.chooseNewsMsg) {// 请求信息栏有没有数据
      util.getNewsMsg()
      app.globalData.chooseNewsMsg = false
    }
  },
  onShow(){
    let that = this
    this.setData({
      student: app.globalData.userInfo
    })
    let user = wx.getStorageSync('userInfo')
    if (user) {
      getStuQuestionnaire(user.cluesOrStuId).then(res => {
        let status = false
        res.quesSurveyList.forEach(item => {
          if (item.isReply == '0' && item.questionnaireState == '0') {
            status = true
          }
        })
        if (status) {
          wx.showTabBarRedDot({
            index: 3
          })
          that.setData({
            hasQuest: true
          })
        } else {
          wx.hideTabBarRedDot({
            index: 3
          })
          that.setData({
            hasQuest: false
          })
        }
      })
      getStuTeacher(user.cluesOrStuId).then(res=>{
        let status = false
        res.quesSurveyList.forEach(item => {
          if (item.is_reply == '0') {
            status = true
          }
        })
        if (status) {
          wx.showTabBarRedDot({
            index: 3
          })
          that.setData({
            hasTeachQuest: true
          })
        } else {
          wx.hideTabBarRedDot({
            index: 3
          })
          that.setData({
            hasTeachQuest: false
          })
        }
      })
    }
  },
  changeAttendanceList() {
    wx.navigateTo({
      url: '/pages/mine/attendanceList/attendanceList'
    })
  },
  changeMeal(){
    wx.navigateTo({
      url: '/pages/mine/meal/meal'
    })
  },
  buyCourse() {
    let userInfo = wx.getStorageSync('userInfo')
    // wx.navigateTo({
    //   url: `/pages/unenter/unenter?cluesOrStuId=${userInfo.cluesOrStuId}&type=${userInfo.type}&account=${userInfo.account}`
    // })
    // wx.navigateToMiniProgram({
    //   appId: 'wx50e32f905911af7b',
    //   path: 'pages/startUp/startUp',
    //   extraData: userInfo,
    //   envVersion: 'release'
    // })
    this.setData({
      userInfo:userInfo
    })
  },
  changeSet(){
    wx.navigateTo({
      url: '/pages/mine/set/set'
    })
  },
  changeUser(){
    wx.navigateTo({
      url: '/pages/mine/user/user'
    })
  },
  questionnaire(){
    wx.navigateTo({
      url: '/pages/questionnaire/questionnaire'
    })
  }
})